package com.carassistant.beta.common.dialog;


import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.carassistant.beta.R;
/****
 * 对话框
 * @author james
 *
 */
public class ViewDialog extends BaseDialog{

	Button sureButton,cancelButton,reserveButton;
	RelativeLayout dialogBottom;
	LinearLayout ll,contentView;
	/**
	 * 构造方法
	 * @param context
	 */
	public ViewDialog(Context context) {
		super(context);

		//初始化视图
		initView();
	}

	public ViewDialog setTitle(String title) {
		if (!TextUtils.isEmpty(title) && null != titleView) {
			titleView.setText(title);
		}
		return this;
	}
	public ViewDialog create() {
		this.show();
		return this;
	}
	/**
	 * 添加用户自定义视图（中间那一块）
	 * @param view
	 * @return
	 */
	public ViewDialog setView(View view) {
		LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);
		contentView.addView(view,llp);
		return this;
	}
	private void initView() {
		ll = (LinearLayout)inflater.inflate(R.layout.viewjar_query_dialog_super_view, null);
		contentView = (LinearLayout)ll.findViewById(R.id.content_view);
		titleView = (TextView)ll.findViewById(R.id.dialog_title);
		sureButton = (Button)ll.findViewById(R.id.dialog_sure);
		cancelButton = (Button)ll.findViewById(R.id.dialog_cancel);
		reserveButton = (Button)ll.findViewById(R.id.dialog_reserve);
		dialogBottom = (RelativeLayout)ll.findViewById(R.id.dialog_bottom);

		WindowManager wManager = (WindowManager) context.getApplicationContext().getSystemService( Context.WINDOW_SERVICE);
		Display display = wManager.getDefaultDisplay();
		int width = display.getWidth();

		LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(
				width - 50,
				LinearLayout.LayoutParams.FILL_PARENT);
		super.setContentView(ll,llp2);
	}
	public void setpaddingBottom(int p){
		contentView.setPadding(contentView.getPaddingLeft(), contentView.getPaddingTop(), contentView.getPaddingRight(), p);
	}
	public void setViewBg(int res) {
		contentView.setBackgroundResource(res);
	}
	/**
	 * 设置标题栏背景
	 * @param res
	 */
	public void setTitleBackground(int res) {
		try {
			if (null != titleView) {
				titleView.setBackgroundResource(res);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ViewDialog setPositiveButton(CharSequence text, OnDialogButtonClickListener listener) {
		dialogBottom.setVisibility(View.VISIBLE);
		sureButton.setVisibility(View.VISIBLE);
		sureButton.setTag(listener);
		sureButton.setOnClickListener(mListener);
		return this;
	}

	public ViewDialog setNegativeButton(CharSequence text, OnDialogButtonClickListener listener) {
		dialogBottom.setVisibility(View.VISIBLE);
		cancelButton.setVisibility(View.VISIBLE);
		cancelButton.setTag(listener);
		cancelButton.setOnClickListener(mListener);
		return this;
	}


	public ViewDialog setNeutralButton(CharSequence text, OnDialogButtonClickListener listener) {
		dialogBottom.setVisibility(View.VISIBLE);
		reserveButton.setVisibility(View.VISIBLE);
		reserveButton.setTag(listener);
		reserveButton.setOnClickListener(mListener);
		return this;
	}


	@Override
	public void setContentView(int layoutResID) {
		View view = inflater.inflate(layoutResID, null);
		this.setContentView(view);
	}
	public ViewDialog setTitleInId(int textResID) {
		String text = context.getResources().getString(textResID);
		setTitle(text);
		return this;
	}
	public ViewDialog setView(int layoutResID) {
		setContentView(layoutResID);
		return this;
	}
	public ViewDialog setPositiveButton(int textId, OnDialogButtonClickListener listener) {
		String text = context.getResources().getString(textId);
		setPositiveButton(text, listener);
		return this;
	}
	public ViewDialog setNegativeButton(int textId, OnDialogButtonClickListener listener) {
		String text = context.getResources().getString(textId);
		setNegativeButton(text, listener);
		return this;
	}
	public ViewDialog setNeutralButton(int textId, OnDialogButtonClickListener listener) {
		String text = context.getResources().getString(textId);
		setNeutralButton(text, listener);
		return this;
	}
}
